//===============================================================================
// Microsoft patterns & practices
// Web Service Software Factory
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Base;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Oracle;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle.Helpers;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle.Data;
using System.Globalization;
#endregion

namespace Microsoft.Practices.RepositoryFactory.SchemaDiscovery.Translators.Oracle
{
    internal static class OracleStoredProcedureTranslator
    {
       /// <summary>
       /// Creates an <see cref="OracleStoredProcedure"/> from a <see cref="DataRow"/>.
       /// </summary>
       /// <param name="OracleStoredProcedure">The <see cref="DataRow"/></param>
       /// <returns>An <see cref="OracleStoredProcedure"/></returns>
       public static OracleStoredProcedure OracleStoredProcedureCollectionToStoreProcedure(DataRow OracleStoredProcedure)
       {

          OracleStoredProcedure storedProcedure = new OracleStoredProcedure();
          string packageName = OracleStoredProcedure["PACKAGE_NAME"].ToString();
          string procedureName = OracleStoredProcedure["object_name"].ToString();
          string seperator = (packageName.Length > 0) ? "." : String.Empty;

          storedProcedure.PackageName = packageName;
          storedProcedure.Name = packageName + seperator + procedureName;

          return storedProcedure;
       }

       /// <summary>
       /// Translates <see cref="DataRow"/> items to a collection of parameters.
       /// </summary>
       /// <param name="OracleParametersCollection"></param>
       /// <returns></returns>
       public static List<Parameter> OracleParametersCollectionToOracleParameters(DataTable OracleParametersCollection)
       {
          if (OracleParametersCollection == null) throw new ArgumentNullException("OracleParametersCollection");

          List<Parameter> parameters = new List<Parameter>(OracleParametersCollection.Rows.Count);

          foreach (DataRow row in OracleParametersCollection.Rows)
          {
             OracleDbParameter parameter = new OracleDbParameter();
             parameter.DBName = row["ARGUMENT_NAME"].ToString();
             parameter.Direction = OracleTranslatorHelper.GetDirection(row["IN_OUT"].ToString());

             OracleDbDataType dbDataType = new OracleDbDataType();
             dbDataType.ProviderType = OracleDataTypeConverter.String2DatabaseType(row["DATA_TYPE"].ToString());
             dbDataType.Type = OracleDataTypeConverter.DatabaseType2NetType(dbDataType.ProviderType);
             dbDataType.Precision = OracleTranslatorHelper.GetPrecision(row["DATA_PRECISION"]);
             dbDataType.Scale = OracleTranslatorHelper.GetScale(row["DATA_SCALE"]);
             dbDataType.Size = OracleTranslatorHelper.GetParameterSize(dbDataType.ProviderType, row);
             parameter.DbDataType = dbDataType;

             parameters.Add(parameter);
          }

          return parameters;
       }
    }
}